#include "mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //【1】窗口本身属性
    this->setMinimumSize(1156,680);
    this->setWindowIcon(QIcon(":/ALL/Image/ALL/Cat2.png"));
    this->setWindowFlags(Qt::FramelessWindowHint);
    CenterWidget = new QWidget(this);
    CenterWidget->setObjectName("CenterWidget");
    this->setCentralWidget(CenterWidget);
    mIsMax = false;//当前并未最大化
    mDrag = false;//当前并未在拖动

    //【2】标题栏
    //1.标题栏本身+下方内容框
    WidgetOfWindowsTitleBar = new QWidget(CenterWidget);
    WidgetOfcontent = new QWidget(CenterWidget);
    WidgetOfWindowsTitleBar->setObjectName("WidgetOfWindowsTitleBar");
    WidgetOfcontent->setObjectName("WidgetOfcontent");
    WidgetOfWindowsTitleBar->setFixedHeight(60);
    LayoutOfAll = new QVBoxLayout(CenterWidget);
    LayoutOfAll->setMargin(0);
    LayoutOfAll->setSpacing(0);
    LayoutOfAll->addWidget(WidgetOfWindowsTitleBar);
    LayoutOfAll->addWidget(WidgetOfcontent);
    CenterWidget->setLayout(LayoutOfAll);
    WidgetOfWindowsTitleBar->installEventFilter(this);
    //2.窗口大小控制按钮
    CloseBtn = new QToolButton(WidgetOfWindowsTitleBar);
    MinimizeBtn = new QToolButton(WidgetOfWindowsTitleBar);
    MaximizeBtn = new QPushButton(WidgetOfWindowsTitleBar);
    CloseBtn->setFixedSize(45,45);
    MaximizeBtn->setFixedSize(45,45);
    MinimizeBtn->setFixedSize(45,45);
    CloseBtn->setObjectName("CloseBtn");
    MinimizeBtn->setObjectName("MinimizeBtn");
    MaximizeBtn->setObjectName("MaximizeBtn");
    MaximizeBtn->setCheckable(true);
    connect(CloseBtn,SIGNAL(clicked()),this,SLOT(close()));
    connect(MinimizeBtn,&QToolButton::clicked,this,&QWidget::showMinimized);
    connect(MaximizeBtn,SIGNAL(clicked()),this,SLOT(SLOTFunOfMaximize()));
    //3.软件LOGO(可跳转至首页)的按钮
    BtnOfLOGO = new QToolButton(WidgetOfWindowsTitleBar);
    BtnOfLOGO->setFixedSize(126,55);
    BtnOfLOGO->setObjectName("BtnOfLOGO");
    //3.返回与刷新按钮
    BtnOfBack = new QToolButton(WidgetOfWindowsTitleBar);
    BtnOfRefer = new QToolButton(WidgetOfWindowsTitleBar);
    BtnOfBack->setFixedSize(30,30);
    BtnOfRefer->setFixedSize(20,20);
    BtnOfBack->setObjectName("BtnOfBack");
    BtnOfRefer->setObjectName("BtnOfRefer");
    //4.搜索框
    WidgetOfResearch = new QWidget(WidgetOfWindowsTitleBar);
    LineeditOfResearch = new QLineEdit(WidgetOfResearch);
    LineeditOfResearch->setObjectName("LineeditOfResearch");
    BtnOfResearch = new QToolButton(WidgetOfResearch);
    BtnOfResearch->setObjectName("BtnOfResearch");
    WidgetOfResearch->setFixedSize(250,60);
    LineeditOfResearch->setGeometry(0,13,190,35);
    BtnOfResearch->setGeometry(154,15,33,31);
    BtnOfResearch->raise();
    //5.历史、收藏按钮
    BtnOfHistory = new QToolButton(WidgetOfWindowsTitleBar);
    BtnOfLover = new QToolButton(WidgetOfWindowsTitleBar);
    BtnOfHistory->setFixedSize(32,32);
    BtnOfLover->setFixedSize(30,30);
    BtnOfHistory->setObjectName("BtnOfHistory");
    BtnOfLover->setObjectName("BtnOfLover");
    //6.头像、消息、日历按钮
    BtnOfMine = new QToolButton(WidgetOfWindowsTitleBar);
    BtnOfCalendar = new QToolButton(WidgetOfWindowsTitleBar);
    BtnOfMessage = new QToolButton(WidgetOfWindowsTitleBar);
    BtnOfCalendar->setFixedSize(30,30);
    BtnOfMine->setFixedSize(30,30);
    BtnOfMessage->setFixedSize(30,30);
    BtnOfMine->setObjectName("BtnOfMine");
    BtnOfMessage->setObjectName("BtnOfMessage");
    BtnOfCalendar->setObjectName("BtnOfCalendar");
    //7.最终布局
    LineeditOfResearch->setFocus();
    LayoutOfWindowsTitleBar = new QHBoxLayout(WidgetOfWindowsTitleBar);
    LayoutOfWindowsTitleBar->setMargin(0);
    LayoutOfWindowsTitleBar->addWidget(BtnOfLOGO);
    LayoutOfWindowsTitleBar->addSpacing(15);
    LayoutOfWindowsTitleBar->addWidget(BtnOfBack);
    LayoutOfWindowsTitleBar->addWidget(BtnOfRefer);
    LayoutOfWindowsTitleBar->addSpacing(15);
    LayoutOfWindowsTitleBar->addWidget(WidgetOfResearch);
    LayoutOfWindowsTitleBar->addStretch();
    LayoutOfWindowsTitleBar->addWidget(BtnOfHistory);
    LayoutOfWindowsTitleBar->addWidget(BtnOfLover);
    LayoutOfWindowsTitleBar->addSpacing(15);
    LayoutOfWindowsTitleBar->addWidget(BtnOfMine);
    LayoutOfWindowsTitleBar->addWidget(BtnOfMessage);
    LayoutOfWindowsTitleBar->addWidget(BtnOfCalendar);
    LayoutOfWindowsTitleBar->addSpacing(15);
    LayoutOfWindowsTitleBar->addWidget(MinimizeBtn);
    LayoutOfWindowsTitleBar->addWidget(MaximizeBtn);
    LayoutOfWindowsTitleBar->addWidget(CloseBtn);
    WidgetOfWindowsTitleBar->setLayout(LayoutOfWindowsTitleBar);

    //【3】左边侧边栏
    //1.节目列表和直播窗口
    WidgetOfProgramList = new QWidget(WidgetOfcontent);
    WidgetOfProgramList->setFixedWidth(230);
    WidgetOfProgramList->setObjectName("WidgetOfProgramList");
    WidgetOfProgramVect = new QWidget(WidgetOfcontent);
    WidgetOfProgramVect->setObjectName("WidgetOfProgramVect");
    LayoutOfContent = new QHBoxLayout(WidgetOfcontent);
    LayoutOfContent->setMargin(0);
    LayoutOfContent->addWidget(WidgetOfProgramList);
    LayoutOfContent->addSpacing(5);
    LayoutOfContent->addWidget(WidgetOfProgramVect);
    WidgetOfcontent->setLayout(LayoutOfContent);
    //2.分类列表和直播进入按钮
    LayoutOfProgramList = new QVBoxLayout(WidgetOfProgramList);
    LayoutOfProgramList->setMargin(0);
    WIdgetOfToolBoxPage1 = new QWidget(WidgetOfProgramList);
    WIdgetOfToolBoxPage2 = new QWidget(WidgetOfProgramList);
    ToolBoxOfProgramList = new QToolBox(WidgetOfProgramList);
    ToolBoxOfProgramList->addItem(WIdgetOfToolBoxPage1,QString::fromUtf8("用户中心\t▶"));
    ToolBoxOfProgramList->addItem(WIdgetOfToolBoxPage2,QString::fromUtf8("直播分类\t▶"));
    BtnOfLiveBroadcast = new QToolButton(WidgetOfProgramList);
    BtnOfLiveBroadcast->setFixedSize(WidgetOfProgramList->width(),50);
//    qDebug()<<WidgetOfProgramList->width();
//    BtnOfLiveBroadcast->setText(QString::fromUtf8("我要直播"));
    LayoutOfProgramList->addWidget(ToolBoxOfProgramList);
    LayoutOfProgramList->addSpacing(5);
    LayoutOfProgramList->addWidget(BtnOfLiveBroadcast);
    WidgetOfProgramList->setLayout(LayoutOfProgramList);


    ToolBoxOfProgramList->setObjectName("ToolBoxOfProgramList");
    WIdgetOfToolBoxPage1->setObjectName("WIdgetOfToolBoxPage");
    WIdgetOfToolBoxPage2->setObjectName("WIdgetOfToolBoxPage");
    BtnOfLiveBroadcast->setObjectName("BtnOfLiveBroadcast");

    //3.分类列表布置----我的页面
    int SizeOfBtns = 200;

    LayoutOfUserCenter = new QVBoxLayout(WIdgetOfToolBoxPage1);

    BtnOfUserCenter = new QPushButton(WIdgetOfToolBoxPage1);
    BtnOfUserHistory = new QPushButton(WIdgetOfToolBoxPage1);
    BtnOfUserLove = new QPushButton(WIdgetOfToolBoxPage1);
    BtnOfUserMoney = new QPushButton(WIdgetOfToolBoxPage1);
    BtnOfUserOeder = new QPushButton(WIdgetOfToolBoxPage1);
    BtnOfUserBroadCastCenter = new QPushButton(WIdgetOfToolBoxPage1);
    BtnOfUserSecuritycenter = new QPushButton(WIdgetOfToolBoxPage1);

    BtnOfUserCenter->setFixedWidth(SizeOfBtns);
    BtnOfUserHistory->setFixedWidth(SizeOfBtns);
    BtnOfUserLove->setFixedWidth(SizeOfBtns);
    BtnOfUserMoney->setFixedWidth(SizeOfBtns);
    BtnOfUserOeder->setFixedWidth(SizeOfBtns);
    BtnOfUserBroadCastCenter->setFixedWidth(SizeOfBtns);
    BtnOfUserSecuritycenter->setFixedWidth(SizeOfBtns);

    LayoutOfUserCenter->addWidget(BtnOfUserCenter);
    LayoutOfUserCenter->addWidget(BtnOfUserHistory);
    LayoutOfUserCenter->addWidget(BtnOfUserLove);
    LayoutOfUserCenter->addWidget(BtnOfUserMoney);
    LayoutOfUserCenter->addWidget(BtnOfUserOeder);
    LayoutOfUserCenter->addWidget(BtnOfUserBroadCastCenter);
    LayoutOfUserCenter->addWidget(BtnOfUserSecuritycenter);

    BtnOfUserCenter->setText(QString::fromUtf8("个人信息"));
    BtnOfUserHistory->setText(QString::fromUtf8("历史记录"));
    BtnOfUserLove->setText(QString::fromUtf8("关注收藏"));
    BtnOfUserMoney->setText(QString::fromUtf8("黑猫钱包"));
    BtnOfUserOeder->setText(QString::fromUtf8("订单中心"));
    BtnOfUserBroadCastCenter->setText(QString::fromUtf8("直播中心"));
    BtnOfUserSecuritycenter->setText(QString::fromUtf8("安全设置"));

    BtnOfUserCenter->setObjectName("UserCenter");
    BtnOfUserHistory->setObjectName("UserCenter");
    BtnOfUserLove->setObjectName("UserCenter");
    BtnOfUserMoney->setObjectName("UserCenter");
    BtnOfUserOeder->setObjectName("UserCenter");
    BtnOfUserBroadCastCenter->setObjectName("UserCenter");
    BtnOfUserSecuritycenter->setObjectName("UserCenter");

    //4.分类列表布置----直播分类
    LayoutOfLiveBroadcastClassification = new QVBoxLayout(WIdgetOfToolBoxPage2);
    BtnOfTheLastLive = new QPushButton(WIdgetOfToolBoxPage2);
    BtnOfTheHotestLive = new QPushButton(WIdgetOfToolBoxPage2);
    BtnOfPhoneGames = new QPushButton(WIdgetOfToolBoxPage2);
    BtnOfWebGames = new QPushButton(WIdgetOfToolBoxPage2);
    BtnOfPCGames = new QPushButton(WIdgetOfToolBoxPage2);
    BtnOfDailyLife = new QPushButton(WIdgetOfToolBoxPage2);
    BtnOfentertainment = new QPushButton(WIdgetOfToolBoxPage2);
    BtnOfRadioStation = new QPushButton(WIdgetOfToolBoxPage2);
    BtnOfVirtualAnchor = new QPushButton(WIdgetOfToolBoxPage2);

    LayoutOfLiveBroadcastClassification->addWidget(BtnOfTheLastLive);
    LayoutOfLiveBroadcastClassification->addWidget(BtnOfTheHotestLive);
    LayoutOfLiveBroadcastClassification->addWidget(BtnOfPhoneGames);
    LayoutOfLiveBroadcastClassification->addWidget(BtnOfWebGames);
    LayoutOfLiveBroadcastClassification->addWidget(BtnOfPCGames);
    LayoutOfLiveBroadcastClassification->addWidget(BtnOfDailyLife);
    LayoutOfLiveBroadcastClassification->addWidget(BtnOfentertainment);
    LayoutOfLiveBroadcastClassification->addWidget(BtnOfRadioStation);
    LayoutOfLiveBroadcastClassification->addWidget(BtnOfVirtualAnchor);

    BtnOfTheLastLive->setFixedWidth(SizeOfBtns);
    BtnOfTheHotestLive->setFixedWidth(SizeOfBtns);
    BtnOfPhoneGames->setFixedWidth(SizeOfBtns);
    BtnOfWebGames->setFixedWidth(SizeOfBtns);
    BtnOfPCGames->setFixedWidth(SizeOfBtns);
    BtnOfDailyLife->setFixedWidth(SizeOfBtns);
    BtnOfentertainment->setFixedWidth(SizeOfBtns);
    BtnOfRadioStation->setFixedWidth(SizeOfBtns);
    BtnOfVirtualAnchor->setFixedWidth(SizeOfBtns);

    BtnOfTheLastLive->setText(QString::fromUtf8("刚刚开播"));
    BtnOfTheHotestLive->setText(QString::fromUtf8("热门直播"));
    BtnOfPhoneGames->setText(QString::fromUtf8("手游"));
    BtnOfWebGames->setText(QString::fromUtf8("网游"));
    BtnOfPCGames->setText(QString::fromUtf8("单口游戏"));
    BtnOfDailyLife->setText(QString::fromUtf8("日常分享"));
    BtnOfentertainment->setText(QString::fromUtf8("娱乐"));
    BtnOfRadioStation->setText(QString::fromUtf8("主播电台"));
    BtnOfVirtualAnchor->setText(QString::fromUtf8("虚拟主播"));

    BtnOfTheLastLive->setObjectName("UserCenter");
    BtnOfTheHotestLive->setObjectName("UserCenter");
    BtnOfPhoneGames->setObjectName("UserCenter");
    BtnOfWebGames->setObjectName("UserCenter");
    BtnOfPCGames->setObjectName("UserCenter");
    BtnOfDailyLife->setObjectName("UserCenter");
    BtnOfentertainment->setObjectName("UserCenter");
    BtnOfRadioStation->setObjectName("UserCenter");
    BtnOfVirtualAnchor->setObjectName("UserCenter");

    //5.右侧预览窗口
    int count[3][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0}};
    QGridLayout *LayoutOfProgramVect = new QGridLayout(WidgetOfProgramVect);
    QWidgetOfPreview *QPreview[3][4];
    for(int i = 0;i < 3;i++)
        for(int j = 0;j<4;j++)
        {
            QPreview[i][j] =new QWidgetOfPreview(WidgetOfProgramVect,count[i][j]);
            LayoutOfProgramVect->addWidget(QPreview[i][j],i,j);
        }

    //【?】最终样式
    QReadQSS::setStyle(":/QSS/QSS/Rester.qss");
}

MainWindow::~MainWindow()
{

}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == WidgetOfWindowsTitleBar)
    {
        if (event->type() == QEvent::MouseButtonDblClick)
        {
            SLOTFunOfMaximize();
            return true;
        }
    }
    return QObject::eventFilter(obj,event);
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && event->y() <= 60)
    {
        mDrag = true;
        mDragPos = event->globalPos() - pos();
        event->accept();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (mDrag && (event->buttons() && Qt::LeftButton) && event->y() <= 60)
    {
        this->showNormal();
        mIsMax = false;
        move(event->globalPos() - mDragPos);
        event->accept();
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    mDrag = false;
}

void MainWindow::SLOTFunOfMaximize()
{
    qDebug()<<mIsMax;
    if(!mIsMax)
    {
//        qDebug()<<"当前未最大化";
        this->showMaximized();
        MaximizeBtn->setObjectName("ReductionBtn");
        QReadQSS::setStyle(":/QSS/QSS/Rester.qss");
        mIsMax = true;
    }
    else
    {
//        qDebug()<<"当前已经最大化";
        this->showNormal();
        MaximizeBtn->setObjectName("MaximizeBtn");
        QReadQSS::setStyle(":/QSS/QSS/Rester.qss");
        mIsMax = false;
    }
}
